﻿using System;
using System.IO;
using System.Runtime.Serialization;
using System.Xml.Serialization;

namespace RRRSRoguelikeData
{
	/// <summary>
	/// Object for storing profile information.
	/// Inherits from <c>ObjectData</c>.
	/// </summary>
	public class Profile : ObjectData
	{
		public Profile():base()
		{
		}
		
		
		//this is here as we only want to load everthing in one step
		public void Load()
		{
			string fileName = GetFileName();
			
			if(File.Exists(fileName))
			{
				try
				{
					var fileStream = File.Open(fileName,FileMode.Open,FileAccess.Read);
					
					DataContractSerializer serializer = new DataContractSerializer(typeof(HighScoreData));
					
					HighScores = (HighScoreData)serializer.ReadObject(fileStream);
				}
				catch(Exception e)
				{
				//HACK: we should write the error here to a log or sth.
//					Console.WriteLine(e);
//					Console.ReadKey();
				}
			}
		}

		string GetFileName()
		{
			return Path.Combine(
				Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), 
				"RRRSRoguelike", "Vanilla", "RRRS.sav");
		}
		
		public void ClearHighScores()
		{
			string fileName = GetFileName();
			
			if(File.Exists(fileName))
				File.Delete(fileName);
		}
		
		//this is here as we only want to load everthing in one step
		public void Save()
		{
			string fileName = GetFileName();
			
			if(!Directory.Exists(Path.GetDirectoryName(fileName)))
			   Directory.CreateDirectory(Path.GetDirectoryName(fileName));
			   
			//HACK: this will always overwrite the save file.
			using( var fileStream = File.Open(fileName,FileMode.Create,FileAccess.ReadWrite))
			      {
			DataContractSerializer serializer = new DataContractSerializer(typeof(HighScoreData));
			
			serializer.WriteObject(fileStream,HighScores);}
		}
		
		public HighScoreData HighScores = new HighScoreData();
		
		public Session Session = new Session();
		
		
		
		
	}
}
